<template>
  <div class="InterviewIssue">
    <div class="InterviewIssue-one">
      <div style="margin-bottom: 40px">岗位: {{ detail.title }}</div>

      <el-tabs v-model="activeName">
        <el-tab-pane label="专业经验" name="first">
          <el-button
            type="primary"
            size="default"
            @click="onAddIssue('专业经验')"
            >添加问题</el-button
          >
          <el-table :data="question['专业经验']" style="width: 100%">
            <el-table-column prop="date" label="问题">
              <template slot-scope="{ row }">{{ row }}</template>
            </el-table-column>
            <el-table-column prop="name" label="操作" width="180">
              <template>
                <el-button type="text" @click="onDelete('专业经验', $index)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="综合能力" name="second">
          <el-button
            type="primary"
            size="default"
            @click="onAddIssue('综合能力')"
            >添加问题</el-button
          >
          <el-table :data="question['综合能力']" style="width: 100%">
            <el-table-column prop="date" label="问题">
              <template slot-scope="{ row }">{{ row }}</template>
            </el-table-column>
            <el-table-column prop="name" label="操作" width="180">
              <template>
                <el-button type="text" @click="onDelete('综合能力', $index)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="价值观" name="third">
          <el-button type="primary" size="default" @click="onAddIssue('价值观')"
            >添加问题</el-button
          >
          <el-table :data="question['价值观']" style="width: 100%">
            <el-table-column prop="date" label="问题">
              <template slot-scope="scope">{{ scope.row }}</template>
            </el-table-column>
            <el-table-column prop="name" label="操作" width="180">
              <template>
                <el-button type="text" @click="onDelete('价值观', scope.$index)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
      <div style="margin-top: 30px">
        <el-button type="primary" size="default" @click="onSave"
          >保 存</el-button
        >
      </div>
    </div>
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      :close-on-click-modal="false"
    >
      <span>
        <el-input
          v-model="issuevalue"
          placeholder="请输入问题"
          size="normal"
          clearable
        ></el-input>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  postAdminRecruitmentInformationGetQuestion,
  postAdminRecruitmentInformationaddQuestion,
} from "@/api";
export default {
  data() {
    return {
      dialogVisible: false,
      issuevalue: "",
      activeName: "first",
      detail: {},
      question: {},
      questionValue: "",
      datadetail: {},
    };
  },
  activated() {
    console.log("detail---->", this.$route.query.detail);
    this.detail = JSON.parse(this.$route.query.detail);
    this.postAdminRecruitmentInformationGetQuestion();
  },
  methods: {
    // 删除
    onDelete(value, index) {
      this.$confirm("确定删除该问题?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.question[value].splice(index, 1);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //   保存
    onSave() {
      let obj;
      if (this.datadetail.id) {
        obj = {
          id: this.datadetail.id,
          position_id: this.detail.id,
          question: JSON.stringify(this.question),
        };
      } else {
        obj = {
          position_id: this.detail.id,
          question: JSON.stringify(this.question),
        };
      }

      postAdminRecruitmentInformationaddQuestion(obj).then((res) => {
        this.$message.success("保存成功");
        this.postAdminRecruitmentInformationGetQuestion()
      });
    },
    //   添加问题弹窗
    onAddIssue(e) {
      this.issuevalue = "";
      this.questionValue = e;
      this.dialogVisible = true;
    },
    // 确认问题关闭弹窗
    onConfirm() {
      if (this.issuevalue) {
        if (!this.question[this.questionValue]) {
          this.question[this.questionValue] = [];
        }
        this.question[this.questionValue].push(this.issuevalue);
        this.dialogVisible = false;
      } else {
        this.$message.warning("请输入问题");
      }
    },
    postAdminRecruitmentInformationGetQuestion() {
      let obj = {
        position_id: this.detail.id,
      };
      postAdminRecruitmentInformationGetQuestion(obj).then((res) => {
        console.log("面试问题列表----------》", res);
        if (res.data.question) {
          this.datadetail = res.data;
          this.question = res.data.question;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.InterviewIssue {
  background: #fff;
  padding: 20px;
  border-radius: 8px;
}
</style>